package com.util.cdb.service.impl;

import com.util.cdb.bean.entity.DbInfo;
import com.util.cdb.bean.entity.Yhml;
import com.util.cdb.bean.vo.dbinfo.DbInfoCreateVO;
import com.util.cdb.bean.vo.yhml.YhmlCreateVO;
import com.util.cdb.dao.DbInfoRepository;
import com.util.cdb.dao.YhmlRepository;
import com.util.cdb.enums.Constants;
import com.util.cdb.exception.DBException;
import com.util.cdb.service.DbInfoService;
import com.util.cdb.service.YhmlService;
import com.util.cdb.util.UUIDTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @description: 数据源管理实现类
 * @author: fengfazheng
 * @create: 2022-01-02 21:12
 **/
@Service
@Slf4j
public class DbInfoServiceImpl implements DbInfoService {
    @Autowired
    private DbInfoRepository dbInfoRepository;
    @Autowired
    private YhmlRepository yhmlRepository;
    @Autowired
    private YhmlService yhmlService;

    /**
     * 新增数据源信息
     *
     * @param dbInfoCreateVO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDbInfo(DbInfoCreateVO dbInfoCreateVO) {
        DbInfo dbInfo = new DbInfo();
        BeanUtils.copyProperties(dbInfoCreateVO, dbInfo);
        String dbId = UUIDTool.getUUID();
        dbInfo.setId(dbId);
        dbInfoRepository.save(dbInfo);
        YhmlCreateVO yhmlCreateVO = new YhmlCreateVO();
        yhmlCreateVO.setUserId(dbInfoCreateVO.getUserId());
        yhmlCreateVO.setMlmc(dbInfoCreateVO.getDbname());
        yhmlCreateVO.setJdlx(Constants.TWO.getValue());
        yhmlCreateVO.setFjdId(dbInfoCreateVO.getFjdId());
        yhmlCreateVO.setDbId(dbId);
        yhmlService.createYhml(yhmlCreateVO);
    }
}
